﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Quickly.Spa.Shared;

namespace Quickly.SpaProxy.ReverseProxy;

internal sealed class SpaReverseProxyMiddleware
{
    public RequestDelegate _next { get; }
    public SpaProxyLaunchManager _spaProxyLaunchManager { get; }
    public IOptions<SpaDevelopmentServerOptions> _options { get; }
    public ILogger<SpaReverseProxyMiddleware> _logger { get; }
    private readonly IHostApplicationLifetime _hostLifetime;

    public SpaReverseProxyMiddleware(
        RequestDelegate next,
        SpaProxyLaunchManager spaProxyLaunchManager,
        IOptions<SpaDevelopmentServerOptions> options,
        IHostApplicationLifetime hostLifetime,
        ILogger<SpaReverseProxyMiddleware> logger)
    {
        _next = next ?? throw new ArgumentNullException(nameof(next));
        _spaProxyLaunchManager = spaProxyLaunchManager ?? throw new ArgumentNullException(nameof(spaProxyLaunchManager));
        _options = options ?? throw new ArgumentNullException(nameof(options));
        _hostLifetime = hostLifetime ?? throw new ArgumentNullException(nameof(hostLifetime));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public Task Invoke(HttpContext context)
    {
        if(MatchSapReverseProxyPath(context))
        {
            return InvokeCore(context);
        }

        return _next(context);
    }

    private Task InvokeCore(HttpContext context)
    {
        throw new NotImplementedException();
    }

    private bool MatchSapReverseProxyPath(HttpContext context)
    {
        throw new NotImplementedException();
    }
}
